package com.hk.kindheartedness;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.model.User;
import com.hk.kindheartedness.model.vo.DoctorVo;
import com.hk.kindheartedness.util.DateUtil;
import com.hk.kindheartedness.util.RegexUtil;
import com.hk.kindheartedness.util.ToastUtil;

import java.util.Date;
import java.util.Objects;

/**
 * 医生个人信息展示且修改
 */
public class DoctorPersonInfoActivity extends AppCompatActivity implements View.OnClickListener {

    //    Initialize Toast
    private Toast toast;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    //  Initialize button
    private Button updatePersonInfoBtn;

    private EditText userName, age, address, specialty, describe;

    private TextView doctorId, phone, idNumber, positionName, departmentsName;

    private RadioButton man, woman;

    @RequiresApi(api = Build.VERSION_CODES.N)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_person_info);
//        实例化
        dbOpenHelper = new DBOpenHelper(DoctorPersonInfoActivity.this, "db_kindheartedness", null, 1);
        // TODO: 2022/3/21 编写查询个人资料并进行解析
        //        获取个人信息控制权
        doctorId = findViewById(R.id.doctorId);
        userName = findViewById(R.id.userName);
        phone = findViewById(R.id.phone);
        idNumber = findViewById(R.id.idNumber);
        age = findViewById(R.id.age);
        address = findViewById(R.id.address);
        specialty = findViewById(R.id.specialty);
        describe = findViewById(R.id.describe);
        positionName = findViewById(R.id.positionName);
        departmentsName = findViewById(R.id.departmentsName);
//         获取性别控制权
        man = findViewById(R.id.man);
        woman = findViewById(R.id.woman);
//        获取更新按钮控制权
        updatePersonInfoBtn = findViewById(R.id.updatePersonInfoBtn);
        updatePersonInfoBtn.setOnClickListener(this);
//        提取出当前用户传递的个人信息
        Intent intent2 = getIntent();
        Bundle bundle = intent2.getExtras();
//        回显获取的个人信息
        echoPersonInfo(bundle);


    }

    /**
     * 回显当前登录用户的个人信息
     *
     * @param bundle
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void echoPersonInfo(Bundle bundle) {
        if (Objects.isNull(bundle)) {
            toast = ToastUtil.toastHint(toast, DoctorPersonInfoActivity.this, "获取个人信息失败,请重试！");
            return;
        }
        String userToString = (String) bundle.get("doctorInfo");
        if (Objects.nonNull(userToString)) {
            DoctorVo doctorVo = JSON.parseObject(userToString, DoctorVo.class);
//                进行回显
            doctorId.setText(doctorVo.getDoctorId().toString());
            userName.setText(doctorVo.getDoctorName());
            phone.setText(doctorVo.getPhone());
            positionName.setText(doctorVo.getPositionName());
            departmentsName.setText(doctorVo.getDepartmentsName());
            idNumber.setText(doctorVo.getIdNumber());
            age.setText(doctorVo.getAge().toString());
//                进行性别判断
            if (doctorVo.getSex() == 0) {
                man.setChecked(true);
            } else {
                woman.setChecked(true);
            }
            address.setText(doctorVo.getAddress());
            specialty.setText(doctorVo.getSpecialty());
            describe.setText(doctorVo.getDescribe());
            return;
        }
        toast = ToastUtil.toastHint(toast, DoctorPersonInfoActivity.this, "获取个人信息失败,请重试！");
        return;

    }


    /**
     * 点击事件处理
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.updatePersonInfoBtn:
//                更新当前登录账户个人信息
                updatePersonInfo();
                break;
            default:
                return;
        }
    }

    /**
     * 更新当前登录账户个人信息
     */
    private void updatePersonInfo() {
        //                更新个人信息
        String doctorId2 = doctorId.getText().toString();
        String doctorUserName = userName.getText().toString();
        String doctorAge = age.getText().toString();
//        判断选择的性别
        String doctorSex = "0";
        if (woman.isChecked()) {
            doctorSex = "1";
        }
        String doctorAddress = address.getText().toString();
        String doctorSpecialty = specialty.getText().toString();
        String doctorDescribe = describe.getText().toString();
        Boolean check = checkPatientData(doctorId2, doctorUserName, doctorAge, doctorSex, doctorAddress, doctorSpecialty, doctorDescribe);
        if (check) {
            SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
//                    封装需要更新的医生账号基本信息
            ContentValues contentValues = new ContentValues();
            contentValues.put("username", doctorUserName);
            contentValues.put("age", doctorAge);
            contentValues.put("sex", doctorSex);
            contentValues.put("address", doctorAddress);
            contentValues.put("update_time", DateUtil.dateString(new Date()));
//                    封装需要更新的医生详细信息
            ContentValues contentDoctorValues = new ContentValues();
            contentDoctorValues.put("username", doctorUserName);
            contentDoctorValues.put("specialty", doctorSpecialty);
            contentDoctorValues.put("describe", doctorDescribe);
            contentDoctorValues.put("update_time", DateUtil.dateString(new Date()));
//                    执行更新医生账号基本信息
            int updateUser = readableDatabase.update("user", contentValues, "doctor_id = ?", new String[]{doctorId2});
//                    执行更新医生详细信息
            int updateDoctor = readableDatabase.update("doctor_details", contentDoctorValues, "id = ?", new String[]{doctorId2});
            if ((updateUser == 1) && (updateDoctor == 1)) {
                toast = ToastUtil.toastHint(toast, DoctorPersonInfoActivity.this, "个人信息更新成功，请返回进行查看！");
                return;
            }
            toast = ToastUtil.toastHint(toast, DoctorPersonInfoActivity.this, "个人信息更新失败，请稍后重试！");
            return;
        }
    }

    /**
     * 校验个人信息
     * @param doctorId2 医生编号
     * @param doctorUserName 姓名
     * @param doctorAge 年龄
     * @param doctorSex 性别
     * @param doctorAddress 地址
     * @param doctorSpecialty 擅长
     * @param doctorDescribe 简介
     * @return
     */
    private Boolean checkPatientData(String doctorId2, String doctorUserName, String doctorAge, String doctorSex, String doctorAddress, String doctorSpecialty, String doctorDescribe) {
        //        校验医生编号
        if (doctorId2.equals("") || doctorId2.equals(null) || doctorId2 == null) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"编号不能为空！");
            return false;
        }
        if (!RegexUtil.checkChinese(doctorUserName)) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"姓名只支持中文！");
            return false;
        }
        if (!RegexUtil.checkDigitz(doctorAge)) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"请输入正确的年龄！");
            return false;
        }
        if (!doctorSex.equals("0") && !doctorSex.equals("1")) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"请选择性别！");
            return false;
        }
        if (doctorAddress.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"请输入地址！");
            return false;
        }

        if (doctorSpecialty.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"请展示你的才华吧！");
            return false;
        }

        if (doctorDescribe.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorPersonInfoActivity.this,"请简单简介一下吧！");
            return false;
        }
        return true;
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy () {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}